<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.defaultNumber"
          clearable
          placeholder="订单编号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.outType"
          placeholder="出庫方式"
          clearable
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.outBillType"
            :key="item.id"
            :label="item.dictValue"
            :value="item.dictKey"
          />
        </el-select>
        <el-select
          v-model="query.audit"
          placeholder="審核狀態"
          clearable
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.audit"
            :key="item.id"
            :label="item.dictValue"
            :value="item.dictKey"
          />
        </el-select>
        <date-range-picker v-model="modelCreatedAt" class="date-item" :fix-date.sync="query.signDate" />
        <!-- 搜索 -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <template slot="left">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="createInventorySurplusOutBill"
          >
            新增報損出库单
          </el-button>
          <el-button
            class="filter-item"
            size="mini"
            type="warning"
            icon="el-icon-s-check"
            :disabled="crud.selections.length < 1"
            @click="auditOutBIll"
          >
            審核
          </el-button>
        </template>
      </crudOperation>
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%" stripe @selection-change="crud.selectionChangeHandler">
        <el-table-column
          type="selection"
          :row-key="getRowKeys"
        />
        <el-table-column
          prop="defaultNumber"
          label="单据号"
        />
        <el-table-column
          prop="outType"
          label="出库类型"
        >
          <template slot-scope="scope">
            {{ dict.label.outBillType[scope.row.outType] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="depot.name"
          label="仓库"
        />
        <el-table-column
          prop="outTime"
          label="出库时间"
        />
        <el-table-column
          prop="operPersonName"
          label="操作员名字"
        />
        <el-table-column
          prop="sumQuantity"
          width="120"
          label="出库总数量	"
        />
        <el-table-column
          prop="sumQuantity"
          width="120"
          label="審核狀態	"
        >
          <template slot-scope="scope">
            {{ dict.label.audit[scope.row.audit] }}
          </template>
        </el-table-column>
        <el-table-column
          align="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="showDetails(scope.row)"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDepotBillOut from '@/api/wms/depotBillOut'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import DateRangePicker from '@/components/DateRangePicker'
const defaultForm = { name: null }
export default {
  name: 'OutBill',
  components: { pagination, rrOperation, crudOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['outBillType', 'audit'],
  cruds() {
    return CRUD({ title: '轮播图', url: 'depotBillOut/page', sort: 'id:desc',
      crudMethod: { ...crudDepotBillOut }, optShow: {
        add: false,
        edit: false,
        del: true,
        download: false,
        reset: false
      },
      query: {
        deleteStatus: false
      }
    })
  },
  data() {
    return {
      modelCreatedAt: '', // 时间选择器显示时间
      ids: [],
      permission: {
        add: ['admin', 'product:add'],
        edit: ['admin', 'product:edit'],
        del: ['admin', 'product:del']
      },
      selectedData: [],
      productId: [],
      queryTypeOptions: [
        { key: 'nameCn', display_name: '中文商品名称' }
      ],
      dialogVisible: false,
      tableData: []
    }
  },
  methods: {
    getModiFyKey() {
      if (this.ids.length > 1) {
        this.$message.error('不能同时修改多条数据')
        return false
      }
      if (this.ids.length === 0) {
        this.$message.error('请选择修改的采购单')
        return false
      }
      return true
    },
    getRowKeys(row) {
      this.ids.push(row.id)
      return row.id
    },
    createInBill() {
      this.$router.push({ path: '/wms/creatOutBill' })
    },
    showDetails(row) {
      const defaultNumber = row.defaultNumber
      this.$router.push({ path: '/wms/outBillDetails', name: 'OutBillDetails', query: { defaultNumber: defaultNumber }})
    },
    createInventorySurplusOutBill() {
      this.$router.push({ path: '/wms/outBill/creatDamagedOutBill', name: 'CreatDamagedOutBill' })
    },
    auditOutBIll() {
      const ids = this.crud.selections.map(item => { return item.id })
      crudDepotBillOut.audit(ids).then(res => {
        this.crud.refresh()
        this.$notify({
          title: res.code === 0 ? '成功' : '失敗',
          message: res.message,
          type: res.code === 0 ? 'success' : 'error'
        })
      })
    }
  }
}
</script>

<style scoped>
</style>
